/**
 * 完成首页部分的action调用
 */
package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.action.result.ClusterVo;
import org.whuims.pois.action.result.DataVo;
import org.whuims.pois.dao.MainPageDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author hysk34
 *         <p/>
 *         2010-10-30
 */
public class MainPageAction extends DispatchAction {

    /**
     * 调用最新新闻，负面新闻，最新主题（今日要点）内的数据分别进行呈现
     * <p/>
     * 集合到一个action中，传多个list，分别进行呈现
     * <p/>
     * newsList，negNewsList(negativeNewsList),topicList
     * <p/>
     * 将三个分别传入reuqest
     */
    private MainPageDao dao = new MainPageDao();

    public ActionForward mainpage(ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response) {
        long topic = getTopic(request);
        if (topic <= 0) {
            return mapping.findForward("login");
        }
        String userName = (String) request.getSession().getAttribute("userName");
        /** 最新新闻 */
        List<DataVo> newsList = dao.getNewest("news", topic, userName);

        /** 负面新闻 */
        List<DataVo> negNewsList = dao.getNegative();

        /** 今日热点 */
        List<ClusterVo> topicList = dao.getTodayList(topic);

        /** 我的关注 */
        List<DataVo> starList = dao.getStarNews(userName, topic);

        /** 焦点关注 */
        //List<FocusBean> focusList = dao.getFocus();

        request.setAttribute("news", newsList);
        request.setAttribute("negNews", negNewsList);
        request.setAttribute("topic", topicList);
//		request.setAttribute("focus", focusList);
        request.setAttribute("star", starList);

        return mapping.findForward("success");
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }
}
